<template>
  <div>
    <el-table
      :data="FoodList"
      :stripe="true"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号">
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="oldPrice"
        label="原价">
      </el-table-column>

      <el-table-column
        prop="price"
        label="优惠价格">
      </el-table-column>

      <el-table-column
        prop="descrption"
        label="描述">
      </el-table-column>

      <el-table-column
        label="操作" width="300px">
        <template slot-scope="scope">
          <el-button @click="deleteById(scope.row.id)"  size="mini" v-if="role==2">删除</el-button>
          <router-link :to="'/comment/CommentList?fid='+scope.row.id" ><el-button size="mini">查看评论</el-button></router-link>
          <router-link :to="'/food/FoodDetail?fid='+scope.row.id"><el-button size="mini">详情</el-button></router-link>
          <el-button size="mini" @click="putCart(scope.row.id)" v-if="role==1">加入购物车</el-button>
        </template>
      </el-table-column>
    </el-table>
    <router-view/>
  </div>
</template>

<script>
    export default {
        name: "FoodList",
      data(){
          return {
            FoodList:[],
            cid:"",
            role:sessionStorage.getItem("role")
          }
      },
      created(){
        this.cid=this.$route.query.cid;
        this.selectAll();
      },
      methods: {
        deleteById(id){
          this.$confirm("是否确认删除","删除提示").then(()=>{
            this.axios.delete("/food/delete/"+id).then((resp)=>{
              this.$message.success("删除成功！");
              this.selectAll();
            });
          });
        },
        selectAll(){
          let url=""
          if(this.cid){
            url="/food/selectByCid?cid="+this.cid;
          }else{
            url="/food/selectAll";
          }
          this.axios.get(url).then((resp)=>{
            this.FoodList=resp.data;
          });
        },
        putCart(bid){
          this.$confirm("是否确认放入购物车","保存提示").then(()=>{
            this.axios.get("/cart/putCart/"+bid).then((resp)=>{
              this.$message.success("保存成功！");
            });
          }).catch(()=>{});
        }
      }
    }
</script>

<style scoped>

</style>
